package com.decent.ejfadmin.user.controller;

import cn.decent.component.shiro.service.AdminService;
import cn.decent.component.shiro.vo.AdminVO;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.WithdrawBank;
import com.decent.ejfadmin.user.bean.BankCodeMap;
import com.decent.ejfadmin.user.bean.Page;
import com.decent.ejfadmin.user.service.UserWithdrawBankService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Objects;

/**
 * describe:会员银行卡信息管理
 *
 * @author JunLei He
 * @date 2018年11月08日
 */
@Slf4j
@RestController
@RequestMapping("UserWithdrawBankController")
public class UserWithdrawBankController {
    @Autowired
    private UserWithdrawBankService userWithdrawBankService;
    @Autowired
    private AdminService adminService;

    /**
     * 查询会员提现银行卡列表
     *
     * @param account 会员编号
     * @param page    分页
     * @param state   银行卡状态
     * @return 返回列表
     */
    @RequestMapping("queryUserWithdrawBankList")
    public Page<WithdrawBank> queryUserWithdrawBankList(String account, Page page, Integer state) {
        try {

            return userWithdrawBankService.queryUserWithdrawBankList(account, page, state);
        } catch (Exception e) {
            log.warn(" 查询会员认证信息异常 ", e);
            page.setList(new ArrayList<WithdrawBank>());
            page.setTotal(0);
            return page;
        }
    }

    /**
     * 修改会员银行卡状态
     *
     * @param bankId 银行卡Id
     * @param state  修改银行卡状态
     * @return 返回列表
     */
    @RequestMapping("updateWithdrawBankState")
    public MessageBean updateWithdrawBankState(Integer bankId, Integer state) {
        try {
            if (Objects.isNull(bankId) || Objects.isNull(state)) {
                return MessageBean.fail("参数错误");
            }
            adminService.getCurrentAdmin();
            return userWithdrawBankService.updateWithdrawBankState(bankId, state);
        } catch (Exception e) {
            log.warn("银行卡id[{}]|状态[{}]查询会员认证信息异常[{}]", bankId, state, e.getMessage(), e);
            return MessageBean.fail(e.getMessage());
        }
    }

    /**
     * 添加会员银行卡
     *
     * @param withdrawBank 银行卡添加信息
     * @return 信息
     */
    @RequestMapping("addWithdrawBank")
    public MessageBean addWithdrawBank(WithdrawBank withdrawBank) {
        try {
            withdrawBank.checkParam();
            AdminVO currentAdmin = adminService.getCurrentAdmin();

            return userWithdrawBankService.addWithdrawBank(withdrawBank, currentAdmin.getName());
        } catch (Exception e) {
            log.warn("添加会员银行卡[{}]异常[{}]", withdrawBank, e.getMessage(), e);
            return MessageBean.fail(e.getMessage());
        }
    }


    /**
     * 查询银行编码映射
     *
     * @param bankCodeMap 银行编码映射
     * @return 返回列表
     */
    @RequestMapping("queryBankCodeMap")
    public Page<BankCodeMap> queryBankCodeMap(Page page, BankCodeMap bankCodeMap) {
        try {

            return userWithdrawBankService.queryBankCodeMap(page, bankCodeMap);
        } catch (Exception e) {
            log.warn(" 查询银行编码映射异常 ", e);
            page.setList(new ArrayList<WithdrawBank>());
            page.setTotal(0);
            return page;
        }
    }

    /**
     * 添加银行卡
     *
     * @param bankCodeMap 银行卡添加信息
     * @return 信息
     */
    @RequestMapping("insertBankCodeMap")
    public MessageBean insertBankCodeMap(BankCodeMap bankCodeMap) {
        try {
            bankCodeMap.checkParam();
            return userWithdrawBankService.insertBankCodeMap(bankCodeMap);
        } catch (Exception e) {
            log.warn("添加银行卡[{}]异常[{}]", bankCodeMap, e.getMessage(), e);
            return MessageBean.fail(e.getMessage());
        }
    }

    /**
     * 修改银行卡
     *
     * @param bankCodeMap 银行卡添加信息
     * @return 信息
     */
    @RequestMapping("updateBankCodeMap")
    public MessageBean updateBankCodeMap(BankCodeMap bankCodeMap) {
        try {
            bankCodeMap.checkParam();
            return userWithdrawBankService.updateBankCodeMap(bankCodeMap);
        } catch (Exception e) {
            log.warn("修改银行卡[{}]异常[{}]", bankCodeMap, e.getMessage(), e);
            return MessageBean.fail(e.getMessage());
        }
    }
}
